//
//  QiniuConf.m
//  XPhoto
//
//  Created by baboy on 2019/6/25.
//  Copyright © 2019 baboy. All rights reserved.
//

#import "QiniuConf.h"
#import "HttpResponse.h"
#import "Mem.h"
#define MEM_KEY_QINIU_CONF @"qiniu_conf"

@implementation QiniuConf
+ (id)defaultConf{
    return [Mem valueForKey:MEM_KEY_QINIU_CONF];
}
+ (QNUploadManager*) defaultUploadManager{
    static id _defaultQiniuUploadManager = nil;
    static dispatch_once_t initOnceQiniuUploadManager;
    dispatch_once(&initOnceQiniuUploadManager, ^{
        QNConfiguration *config = [QNConfiguration build:^(QNConfigurationBuilder *builder) {
            builder.useHttps = YES;// 是否使用https
//            builder.zone = [QNFixedZone zone0];// 指定华东区域
            // builder.zone = [QNFixedZone zone1];// 指定华北区域
            // builder.zone = [QNFixedZone zone2];// 指定华南区域
            // builder.zone = [QNFixedZone zoneNa0];// 指定北美区域
            // builder.zone = [QNFixedZone zoneAs0];// 指定东南亚区域
        }];
        _defaultQiniuUploadManager = [[QNUploadManager alloc] initWithConfiguration:config];
    });
    
    return _defaultQiniuUploadManager;
}
+ (id)queryConfWithCallback:(nullable void (^)(id _Nonnull task, id _Nullable json, NSError * _Nullable error))callback{
    id task = [BKApi request:@"qiniu_conf_query"
                parameters:nil
                  callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
                      if (error) {
                          callback(task, json, error);
                          return;
                      }
                      HttpResponse *response = [HttpResponse responseWithDictionary:json dataItemClass:[QiniuConf class]];
                      if([response isSuccess]){
                          [Mem setValue:response.data forKey:MEM_KEY_QINIU_CONF];
                      }
                  }];
    return task;
}

+ (void)uploadFile:(NSString*)fp
               fileName:(NSString*)fn
          progress:(void(^)(NSString *key, float percent))progress
        completion:(void (^)(QNResponseInfo *info, NSString *key, NSDictionary *resp))callback{
    QNUploadManager *uploadManager = [self defaultUploadManager];
    QNUploadOption *uploadOption = [[QNUploadOption alloc] initWithProgressHandler:^(NSString *key, float percent) {
        if (progress) {
            progress(key, percent);
        }
    }];
    NSString *key = fn;
    if ([[key substringToIndex:1] isEqualToString:@"/"]) {
        key = [key substringFromIndex:1];
    }
    [uploadManager putFile:fp
                   key:key
                 token:[[self defaultConf] token]
              complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
                  NSLog(@"info ===== %@", info);
                  NSLog(@"resp ===== %@", resp);
                  if (callback) {
                      callback(info, key, resp);
                  }
    }
                option:uploadOption];
    
}
+ (void)uploadPHAsset:(PHAsset*)phAsset
               fileName:(NSString*)fn
          progress:(void(^)(NSString *key, float percent))progress
        completion:(void (^)(QNResponseInfo *info, NSString *key, NSDictionary *resp))callback{
    
    QNUploadManager *uploadManager = [self defaultUploadManager];
    QNUploadOption *uploadOption = [[QNUploadOption alloc] initWithProgressHandler:^(NSString *key, float percent) {
        if (progress) {
            progress(key, percent);
        }
    }];
    NSString *key = fn;
    if ([[key substringToIndex:1] isEqualToString:@"/"]) {
        key = [key substringFromIndex:1];
    }
    [uploadManager putPHAsset:phAsset
                          key:key
                        token:[[self defaultConf] token]
                     complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
        NSLog(@"info ===== %@", info);
        NSLog(@"resp ===== %@", resp);
        if (callback) {
            callback(info, key, resp);
        }
    } option:uploadOption];
}
@end
